package com.sxm.yiwei.activitis;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.sxm.yiwei.MainActivity;
import com.sxm.yiwei.R;
import com.sxm.yiwei.adapters.OrderAdapter;
import com.sxm.yiwei.adapters.YivierSelectAdapter;
import com.sxm.yiwei.tools.ErrType;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class YiViListActivity extends BaseActivity implements View.OnClickListener, AMapLocationListener {

    private ImageView yiviback;
    private ListView yivilist;
    private SharedPreferences sharedPreferences;
    private String appId;
    private String userId;
    private String token;
    private String id;
    private double latitude1;
    private double longitude1;
    private List<HashMap<String,String>>list = new ArrayList();
    private HashMap<String,String> map;
    private ImageView backmain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yi_vi_list);
        sharedPreferences = getSharedPreferences("userlogin", Context.MODE_PRIVATE);
        appId = sharedPreferences.getString("appid", "0");
        userId = sharedPreferences.getString("userId", "1");
        token = sharedPreferences.getString("token", "2");
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        initYiViList();
        getLoc();
        getYiVierData();
    }
    // 获取位置信息 经纬度
    public void getLoc() {
        AMapLocationClient client = new AMapLocationClient(YiViListActivity.this);
        AMapLocationClientOption option = new AMapLocationClientOption();
        option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        option.setInterval(300000);
        client.setLocationListener(this);
        client.setLocationOption(option);
        client.startLocation();
        latitude1 = client.getLastKnownLocation().getLatitude();
        longitude1 = client.getLastKnownLocation().getLongitude();



    }

    private void getYiVierData() {
        RequestParams params = new RequestParams(InternetAddressUtis.Url + InternetAddressUtis.YIVIERLIST);
        params.addHeader("Content-Type", "application/json");
        params.addHeader("token", token);
        params.addHeader("appId", appId);
        params.addHeader("userId", userId);
        JSONObject j = new JSONObject();
        j.put("orderId", id);
        j.put("longitude", String.valueOf(longitude1));
        j.put("latitude", String.valueOf(latitude1));
        params.addParameter("params", j.toJSONString());
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                JSONObject object = JSON.parseObject(result);
                String data = object.getString("data");

                JSONObject object1 = JSON.parseObject(data);
                String courierList = object1.getString("courierList");

                JSONArray jsonArray = JSONArray.parseArray(courierList);

                for (int i = 0; i < jsonArray.size(); i++) {
                    map = new HashMap<String, String>();

                    JSONObject obj = JSON.parseObject(String.valueOf(jsonArray.get(i)));

                    String distance = obj.getString("distance");
                    String gradeCode = obj.getString("gradeCode");
                    String nickname = obj.getString("nickname");
                    String orderTakenCount = obj.getString("orderTakenCount");
                    final String userId = obj.getString("userId");
                    map.put("distance",distance);
                    map.put("gradeCode",gradeCode);
                    map.put("nickname",nickname);
                    map.put("orderTakenCount",orderTakenCount);
                    map.put("userId",userId);
                    list.add(map);
                    final YivierSelectAdapter adapter = new YivierSelectAdapter(YiViListActivity.this,list);
                    yivilist.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                    adapter.setOnClick(new YivierSelectAdapter.Callback() {
                        @Override
                        public void onclick(int v) {
                            HashMap<String,String> item = (HashMap<String, String>) adapter.getItem(v);
                            String userId1 = item.get("userId");
                            selectYivier(userId1);
                        }

                        @Override
                        public void onclick1(int v) {
                            finish();
                        }
                    });
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(YiViListActivity.this,ex);

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
        // 选定一喂员
    private void selectYivier(String u) {
        RequestParams params = new RequestParams(InternetAddressUtis.Url + InternetAddressUtis.SELECTYIVIER);
        params.addHeader("Content-Type", "application/json");
        params.addHeader("token", token);
        params.addHeader("appId", appId);
        params.addHeader("userId", userId);
        JSONObject j = new JSONObject();
        j.put("orderId",id);
        j.put("courierId",u);
        params.setBodyContent(j.toJSONString());

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                finish();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ErrType.errType(YiViListActivity.this,ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initYiViList() {
        yiviback = (ImageView) findViewById(R.id.yivilist_back);
        yivilist = (ListView) findViewById(R.id.yivilist);
        backmain = (ImageView) findViewById(R.id.yivilist_backmain);
        backmain.setOnClickListener(this);
        yiviback.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.yivilist_back:

                finish();
                break;
            case R.id.yivilist_backmain:
                startActivity(new Intent(YiViListActivity.this,MainActivity.class));
                AppManager.getAppManager().finishAllActivity();
                break;
        }

    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {

    }
}
